package com.my.attendance.controller;

import com.my.attendance.entity.Department;
import com.my.attendance.entity.Employee;
import com.my.attendance.entity.History;
import com.my.attendance.entity.Position;
import com.my.attendance.service.DepartmentService;
import com.my.attendance.service.EmployeeService;
import com.my.attendance.service.HistoryService;
import com.my.attendance.service.PositionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/PreUpdateEmployeeServlet")
public class PreUpdateEmployeeServlet extends HttpServlet {
    private DepartmentService departmentService=new DepartmentService();
    private PositionService positionService=new PositionService();
    private EmployeeService employeeService=new EmployeeService();
    private HistoryService historyService=new HistoryService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");
            Employee employee = employeeService.getEmployee(Integer.parseInt(id));
            req.setAttribute("employee",employee);
            List<Department> departmentList = departmentService.getDepartmentList();
            req.setAttribute("departmentList",departmentList);
            List<Position> positionList = positionService.getPositionList();
            req.setAttribute("positionList",positionList);
            Integer employee_number = employee.getEmployee_number();
            History history = historyService.getHistory(employee_number);
            req.setAttribute("history",history);
            req.getRequestDispatcher("jsp/admin/employee_update.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
